package com.example.btcontroller;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.os.Bundle;
import android.widget.Toast;

import java.io.IOException;

public class config_par_activity extends AppCompatActivity {
    public static final char modbus_multiple_write = 0x10;
    public static final char a_b_dis_addr = 0x1f;
    public static  EditText a_b_dis_bias_EditText;
    public static  EditText a_b_l_par_EditText;

    public static  EditText a_temp_bias_EditText;
    public static  EditText b_temp_bias_EditText;

    public static  EditText a_dis_bias_EditText;
    public static  EditText b_dis_bias_EditText;

    public static  EditText AV_k_EditText;
    public static  EditText section_area_EditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_par);

        a_b_dis_bias_EditText = (EditText) findViewById(R.id.a_b_dis_bias_EditText);
        a_b_l_par_EditText = (EditText) findViewById(R.id.a_b_l_par_EditText);

        a_temp_bias_EditText = (EditText) findViewById(R.id.a_temp_bias_EditText);
        b_temp_bias_EditText = (EditText) findViewById(R.id.b_temp_bias_EditText);

        a_dis_bias_EditText = (EditText) findViewById(R.id.a_dis_bias_EditText);
        b_dis_bias_EditText = (EditText) findViewById(R.id.b_dis_bias_EditText);

        AV_k_EditText = (EditText) findViewById(R.id.AV_k_EditText);
        section_area_EditText = (EditText) findViewById(R.id.section_area_EditText);

        double df;
        df = (float)MainActivity.uCSB_PAR.a_b_dis_bias;a_b_dis_bias_EditText.setText(String.format("%.3f",df));
        df = (float)MainActivity.uCSB_PAR.a_b_l_par;a_b_l_par_EditText.setText(String.format("%.3f",df));

        df = (float)MainActivity.uCSB_PAR.a_temp_bias;a_temp_bias_EditText.setText(String.format("%.3f",df));
        df = (float)MainActivity.uCSB_PAR.b_temp_bias;b_temp_bias_EditText.setText(String.format("%.3f",df));

        df = (float)MainActivity.uCSB_PAR.a_dis_bias;a_dis_bias_EditText.setText(String.format("%.3f",df));
        df = (float)MainActivity.uCSB_PAR.b_dis_bias;b_dis_bias_EditText.setText(String.format("%.3f",df));

        df = (float)MainActivity.uCSB_PAR.AV_k;AV_k_EditText.setText(String.format("%.3f",df));
        df = (float)MainActivity.uCSB_PAR.section_area;section_area_EditText.setText(String.format("%.3f",df));

        //发送按钮响应
        Button WriteButton = (Button) findViewById(R.id.WriteButton);
        WriteButton.setOnClickListener(new View.OnClickListener() {
                                           public void onClick(View v) {
                                               // int n = 0;
                                               if (MainActivity.mBluetoothSocket == null) {
                                                   Toast.makeText(getApplicationContext(), "请先连接设备", Toast.LENGTH_SHORT).show();
                                                   return;
                                               }


                                               byte[] bArray = new byte[41];
                                               bArray[0] = (byte)0xff;//设备地址
                                               bArray[1] = (byte)modbus_multiple_write;//功能码
                                               bArray[2] = 0x41;                       //起始地址高字节
                                               bArray[3] = (byte)a_b_dis_addr;         //起始地址低字节
                                               bArray[4] = 0x00;                       //寄存器数量
                                               bArray[5] = 8;                          //寄存器数量
                                               bArray[6] = 0x20;                       //字节数
                                               //32字节
                                               byte[] float_b = new byte[4];
                                               float x;

                                               x = Float.valueOf(a_b_dis_bias_EditText.getText().toString());
                                               float_b = MainActivity.float2byte(x);

                                               int index = 7;
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据

                                                x = Float.valueOf(a_b_l_par_EditText.getText().toString());
                                                float_b = MainActivity.float2byte(x);
                                               //index 11
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据

                                               x = Float.valueOf(AV_k_EditText.getText().toString());
                                                float_b = MainActivity.float2byte(x);
                                               //index 15
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据

                                               x = Float.valueOf(a_temp_bias_EditText.getText().toString());
                                               float_b = MainActivity.float2byte(x);
                                               //index 19
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据

                                                x = Float.valueOf(b_temp_bias_EditText.getText().toString());
                                                float_b = MainActivity.float2byte(x);
                                               //index 23
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据

                                                x = Float.valueOf(a_dis_bias_EditText.getText().toString());
                                                float_b = MainActivity.float2byte(x);
                                               //index 27
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据

                                                x = Float.valueOf(b_dis_bias_EditText.getText().toString());
                                                float_b = MainActivity.float2byte(x);
                                               //index 31
                                               //                bArray[ind6ex++] = float_b[index-26];//数据
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据
                                                x = Float.valueOf(section_area_EditText.getText().toString());
                                               float_b = MainActivity.float2byte(x);
                                               //index 35
                                               bArray[index++] = float_b[0];//数据
                                               bArray[index++] = float_b[1];//数据
                                               bArray[index++] = float_b[2];//数据
                                               bArray[index++] = float_b[3];//数据
                                               //index 39

                                               int y = MainActivity.CRC.crc16(bArray,index);

                                               bArray[index++] = (byte)(y>>8);//crc
                                               bArray[index++] = (byte)y;//crc

                                               MainActivity.blu_send_hex(bArray);
                                               Toast.makeText(getApplicationContext(), "设定成功！", Toast.LENGTH_SHORT).show();
                                           }
                                       }
        );
        //发送按钮响应
        Button ret_Button = (Button) findViewById(R.id.ret_Button);
        ret_Button.setOnClickListener(new View.OnClickListener() {
                                           public void onClick(View v) {
                                            finish();
                                           }
                                       }
        );
    }//end of on create
    //关闭程序掉用处理部分,恢复实时采集读取
    public void onDestroy(){
        super.onDestroy();
        MainActivity.uCSB_PAR.startstop_f = 1;
    }
}